# -*- coding: utf-8 -*-
from datetime import timedelta

from utils.operators.spark_submit import SparkSubmitOperator
from contextlib import ExitStack

from jms.dwd import jms_dwd__dwd_yl_oms_oms_waybill_incre_dt
from jms.ods import jms_ods__yl_tms_vehicle_type, jms_ods__yl_tmsnew_tms_vehicle_line, jms_ods__sys_manage_region

jms_dwd__dwd_centersend_timerate_detail_dt_bak = SparkSubmitOperator(
    task_id='jms_dwd__dwd_centersend_timerate_detail_dt_bak',
    email=['wangmenglei@jtexpress.com','yl_bigdata@yl-scm.com'],
    pool_slots=4,
    # depends_on_past=True,  # 如果任务依赖于前一天的同名任务，则将 depends_on_past 设为 True
    task_concurrency=1,  # 如果任务不支持并发，则将 task_concurrency 设为 1
    name='jms_dwd__dwd_centersend_timerate_detail_dt_bak{{ execution_date | date_add(1) | cst_ds }}',  # yarn 任务名称
    driver_memory='14G',
    driver_cores=2,
    executor_memory='25G',
    executor_cores=4,
    num_executors=100,
    conf={'spark.executor.memoryOverhead': 2048,
          'spark.core.connection.ack.wait.timeout': 300,
          'spark.default.parallelism': 600,
          'spark.locality.wait': 60,
          },
    # jars='hdfs:///user/oozie/chenhoukun/report/centersend_timerate_data/pro/common-1.0-SNAPSHOT.jar',  # 依赖 jar 包
    jars='hdfs:///scheduler/jms/spark/chk/report/centersend_timerate_data/pro/common-1.0-SNAPSHOT.jar',  # 依赖 jar 包
    java_class='com.yunlu.bigdata.jobs.report.CenterSendTimerateDetailData',  # spark 主类
    application='hdfs:///scheduler/jms/spark/chk/report/centersend_timerate_data/pro/original-jobs-1.0-SNAPSHOT.jar',
    # application='hdfs:///user/oozie/chenhoukun/report/centersend_timerate_data/pro/original-jobs-1.0-SNAPSHOT.jar',  # spark jar 包
    # spark jar 包
    application_args=['{{ execution_date  | date_add(1)| cst_ds }}'],  # 参数dt 2020-10-26
    execution_timeout=timedelta(hours=1),
)

# 设置上游依赖
def set_deps():
    from jms.dim import jms_dim__dim_sys_network_detail_dt
    from jms.dm import jms_dm__dm_missing_bind_dt
    from jms.dwd import jms_dwd__dwd_barscan_centerarrival_dt, jms_dwd__dwd_barscan_unloading_dt, jms_dwd__dwd_oms_order_dt, \
        jms_dwd__dwd_oms_waybill_dt
    from jms.dws import jms_dws__dws_actual_aging_sign_dt
    from jms.ods import jms_ods__tab_barscan_centersend, jms_ods__tab_barscan_centerarrival, jms_ods__tab_barscan_loading, \
        jms_ods__yl_oms_interceptorpiece, jms_ods__tab_reback_transfer_express, jms_ods__tab_barscan_difficult, \
        jms_ods__tab_statistics, jms_ods__yl_lmdm_sys_second_code, jms_ods__yl_lmdm_sys_first_code, \
        jms_ods__yl_tmsnew_branch_shipment, jms_ods__yl_tmsnew_branch_shipment_stop, jms_ods__yl_tmsnew_tms_shipment, \
        jms_ods__yl_tmsnew_tms_shipment_stop, jms_ods__tab_bulk_cargo_shift

# 设置依赖
    jms_dwd__dwd_centersend_timerate_detail_dt_bak << [
        jms_dim__dim_sys_network_detail_dt,
        jms_ods__tab_barscan_centersend,
        jms_dwd__dwd_barscan_centerarrival_dt,
        jms_ods__tab_barscan_centerarrival,
        jms_ods__tab_barscan_loading,
        jms_dwd__dwd_barscan_unloading_dt,
        jms_dm__dm_missing_bind_dt,
        jms_ods__yl_oms_interceptorpiece,
        jms_ods__tab_reback_transfer_express,
        jms_ods__tab_barscan_difficult,
        jms_dws__dws_actual_aging_sign_dt,
        jms_dwd__dwd_oms_order_dt,
        jms_ods__tab_statistics,
        jms_dwd__dwd_yl_oms_oms_waybill_incre_dt,
        jms_ods__yl_lmdm_sys_second_code,
        jms_ods__yl_lmdm_sys_first_code,
        jms_ods__yl_tmsnew_branch_shipment,
        jms_ods__yl_tmsnew_branch_shipment_stop,
        jms_ods__yl_tmsnew_tms_shipment,
        jms_ods__yl_tmsnew_tms_shipment_stop,
        jms_ods__tab_bulk_cargo_shift,
        jms_ods__yl_tms_vehicle_type,
        jms_ods__yl_tmsnew_tms_vehicle_line,
        jms_ods__sys_manage_region,
    ]

# 延迟执行设置上游依赖，避免 import python 包的时候其他 Operator 尚未加载完成引起的循环依赖错误
with ExitStack() as stack:
    stack.callback(set_deps)
